package appModel;

import gridModel.GridSize;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.util.List;

import appModel.GridsCollection.Categories;
import appModel.GridsCollection.Quota;

public class StdSubGalleryModel implements SubGalleryModel {
	
	// ATTRIBUTS
	
	private final GridsCollection collection = GridsCollection.getGridsCollection(this);
	
	private PropertyChangeSupport listeners;
	
	private final Categories category;
	private final GridSize gridSize;
	private boolean displayed;
	private boolean gameOn;
	
	// CONSTRUCTEUR
	
	public StdSubGalleryModel(Categories c, GridSize g) {
		category = c;
		gridSize = g;
		setDisplayed(false);
		gameOn = false;
	}
	
	// REQUETES
	
	@Override
	public boolean isGameOn() {
		return gameOn;
	}
	
	@Override
	public boolean isDisplayed() {
		return displayed;
	}

	@Override
	public Categories getCategory() {
		return category;
	}

	@Override
	public GridSize getGridSize() {
		return gridSize;
	}

	@Override
	public List<String> getFiles() {
		return collection.getGrids(category, gridSize);
	}
	
	@Override
	public Quota getQuota() throws IOException {
		return collection.getSize(category, gridSize);
	}

	@Override
	public PropertyChangeListener[] getPropertyChangeListeners() {
		if (listeners == null) {
			return null;
		}
		return listeners.getPropertyChangeListeners();
	}
	
	// COMMANDES
	
	@Override
	public void setGameOn(boolean gameOn) {
		boolean old = this.gameOn;
		this.gameOn = gameOn;
		if (listeners != null) {
			listeners.firePropertyChange("gameOn", old, gameOn);
		}
	}
	
	@Override
	public void setDisplayed(boolean display) {
		boolean old = displayed;
		displayed = display;
		if (listeners != null) {
			listeners.firePropertyChange("displayed", old, displayed);
		}
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener l) {
		if (l == null) {
			throw new IllegalArgumentException();
		}
		if (listeners == null) {
			listeners = new PropertyChangeSupport(this);
		}
		listeners.addPropertyChangeListener(l);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener l) {
		if (l == null) {
			throw new IllegalArgumentException();
		}
		listeners.removePropertyChangeListener(l);
	}

}
